package com.xquant.databasemanagement.controller.panel.left;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xquant.databasemanagement.bean.CommonObject;
import com.xquant.databasemanagement.bean.DataBaseObject;
import com.xquant.databasemanagement.bean.SequenceBean;
import com.xquant.databasemanagement.bean.table.TableBean;
import com.xquant.databasemanagement.config.Config;
import com.xquant.databasemanagement.config.ConstantConfig;
import com.xquant.databasemanagement.controller.panel.center.CommonPanelController;
import com.xquant.databasemanagement.controller.panel.center.ComparePanelController;
import com.xquant.databasemanagement.controller.panel.center.SequencePanelController;
import com.xquant.databasemanagement.controller.panel.center.TablePanelController;
import com.xquant.databasemanagement.service.CompareService;
import com.xquant.databasemanagement.service.XMLService;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

import java.net.URL;
import java.util.ResourceBundle;

public class LeftPanelController implements Initializable {

    public TreeItem procedure;
    public TreeItem function;
    public TreeItem trigger;
    public TreeItem basicData;
    public TreeItem view;
    public TreeItem sequence;
    public TreeItem table;

    @FXML
    private Pane comparePanel;

    @FXML
    private VBox tablePanel;

    @FXML
    private VBox commonPanel;

    @FXML
    private VBox sequencePanel;

    @FXML
    public TreeView treeView;
    private ContextMenu menu;
    private MenuItem menuItem;
    private BorderPane mainPanel;

    @FXML
    private CommonPanelController commonPanelController;

    @FXML
    private SequencePanelController sequencePanelController;

    @FXML
    private TablePanelController tablePanelController;

    @FXML
    private ComparePanelController comparePanelController;

    public CompareService compareService;

    public XMLService xmlService;



    @FXML
    private void selectItem(MouseEvent event) throws JsonProcessingException {
        TreeItem treeItem = (TreeItem) treeView.getSelectionModel().getSelectedItem();
        mainPanel = (BorderPane)treeView.getParent().getParent();
        treeView.setContextMenu(null);
        if (Config.isOpenCompareData) {
            if (treeItem != null &&event.getButton().equals(MouseButton.PRIMARY)
                    &&event.getClickCount()==2&&(treeItem.getValue() instanceof DataBaseObject)  ) {
               mainPanel.setCenter(comparePanel);
               updateCenterPanel((String) treeItem.getParent().getValue(),treeItem);

            }
            return;
        }
        if (event.getButton().equals(MouseButton.PRIMARY)&&event.getClickCount()==2) {
            if (treeItem != null &&treeItem.getParent().getValue()!=null&&
                    (treeItem.getChildren() == null || treeItem.getChildren().size() == 0)) {
                String parentValue = (String) treeItem.getParent().getValue();
                updateCenterPanel(parentValue,treeItem);
            }
        }else if (event.getButton().equals(MouseButton.SECONDARY)){
            if (treeItem!=null){
                if ("数据库对象".equals((String) treeItem.getParent().getValue())||
                        (!(treeItem.getValue() instanceof DataBaseObject) &&"初始化数据".equals((String) treeItem.getValue()))){
                    menuItem.setText("添加");
                    treeView.setContextMenu(menu);
                }else if (treeItem.getChildren() == null || treeItem.getChildren().size() == 0){
                    menuItem.setText("删除");
                    treeView.setContextMenu(menu);
                }
            }
        }

    }

    public void initMenu(){
        menu = new ContextMenu();
        menuItem = new MenuItem();
        menu.getItems().addAll(menuItem);
        menuItem.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                TreeItem treeItem = (TreeItem) treeView.getSelectionModel().getSelectedItem();
                if (menuItem.getText().equals("添加")){
                    try {
                        updateCenterPanel((String) treeItem.getValue(),treeItem);
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }else if(menuItem.getText().equals("删除")){
                    treeItem.getParent().getChildren().remove(treeItem);
                    mainPanel.setCenter(null);
                }
            }
        });
    }



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initMenu();
        commonPanelController.setTreeView(treeView);
        tablePanelController.setTreeView(treeView);
        sequencePanelController.setTreeView(treeView);
    }


    private void updateCenterPanel(String treeItemValue,TreeItem treeItem) throws JsonProcessingException {
        switch (treeItemValue) {
            case "表":
                if (Config.isOpenCompareData){
                    compareXml(ConstantConfig.TABLE,ConstantConfig.COMPARE_TABLE,treeItem.getValue().toString());
                    break;
                }
                tablePanelController.clear();
                if (treeItem.getValue() instanceof DataBaseObject){
                    tablePanelController.setTableBean((TableBean) treeItem.getValue());
                }else {
                    tablePanelController.setTreeItem((TreeItem) treeView.getSelectionModel().getSelectedItem());
                }
                mainPanel.setCenter(tablePanel);
                break;
            case "存储过程":
                if (Config.isOpenCompareData){
                    compareXml(ConstantConfig.PROCEDURE,ConstantConfig.COMPARE_PROCEDURE,treeItem.getValue().toString());
                    break;
                }
            case "触发器":
                if (Config.isOpenCompareData){
                    compareXml(ConstantConfig.TRIGGER,ConstantConfig.COMPARE_TRIGGER,treeItem.getValue().toString());
                    break;
                }
            case "视图":
                if (Config.isOpenCompareData){
                    compareXml(ConstantConfig.VIEW,ConstantConfig.COMPARE_VIEW,treeItem.getValue().toString());
                    break;
                }
            case "函数":
                if (Config.isOpenCompareData){
                    compareXml(ConstantConfig.FUNCTION,ConstantConfig.COMPARE_FUNCTION,treeItem.getValue().toString());
                    break;
                }
            case "初始化数据":
                if (Config.isOpenCompareData){
                    compareXml(ConstantConfig.BASICDATA,ConstantConfig.COMPARE_BASICDATA,treeItem.getValue().toString());
                    break;
                }
                commonPanelController.clear();
                if (treeItem.getValue() instanceof DataBaseObject){
                    commonPanelController.setCommonBean((CommonObject) treeItem.getValue());
                }else {
                    commonPanelController.setTreeItem((TreeItem) treeView.getSelectionModel().getSelectedItem());
                }
                mainPanel.setCenter(commonPanel);
                break;
            case "序列":
                if (Config.isOpenCompareData){
                    compareXml(ConstantConfig.SEQUENCE,ConstantConfig.COMPARE_SEQUENCE,treeItem.getValue().toString());
                    break;
                }
                sequencePanelController.clear();
                if (treeItem.getValue() instanceof DataBaseObject){
                    sequencePanelController.setSequenceBean((SequenceBean) treeItem.getValue());
                }else {
                    sequencePanelController.setTreeItem((TreeItem) treeView.getSelectionModel().getSelectedItem());
                }
                mainPanel.setCenter(sequencePanel);
                break;
        }
    }

    private void compareXml(String key,String compareKey,String beanName) throws JsonProcessingException {
        comparePanelController.xml.clear();
        comparePanelController.compareXml.clear();
        Object obj = compareService.getCompareData(key).get(beanName);
        Object compareObj = compareService.getCompareData(compareKey).get(beanName);
        if (obj == null)
            comparePanelController.compareXml.setText(xmlService.createXML((DataBaseObject) compareObj));
        else if (compareObj == null)
            comparePanelController.xml.setText(xmlService.createXML((DataBaseObject) obj));
        else {
            comparePanelController.xml.setText(xmlService.createXML((DataBaseObject) obj));
            comparePanelController.compareXml.setText(xmlService.createXML((DataBaseObject) compareObj));
        }

    }



    public void clearControl(){
        table.getChildren().clear();
        view.getChildren().clear();
        function.getChildren().clear();
        trigger.getChildren().clear();
        sequence.getChildren().clear();
        basicData.getChildren().clear();
        procedure.getChildren().clear();
    }




}
